package com.easy.common.util;
import com.easy.common.exception.ExceptionEnum;

/**
 * @Author: CoolPro
 * @Description: 响应处理工具
 * @Time: 2024-03-21 18:38
 **/
public class Result<T> {

    private Integer code; //状态码
    String msg; //状态信息
    private T data; // 携带参数


    /**
     * @Author: CoolPro
     * @Date: 22/3/2024
     * @Description: 请求成功时使用的方法,不携带参数,只返回状态码和状态信息
     * @Name: success
     * @parmas: []
     * @return: com.qidian.util.Result<T>
    */
    public static <T> Result<T> success() {
        Result<T> result = new Result<T>();
        result.setStatus(ExceptionEnum.OK);
        return result;
    }

    /**
     * @Author: CoolPro
     * @Date: 22/3/2024
     * @Description: 请求成功时使用的方法,携带了参数信息
     * @Name: success
     * @parmas: [data]
     * @return: com.qidian.util.Result<T>
    */
    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<T>(data);
        result.setStatus(ExceptionEnum.OK);
        return result;
    }


    /**
     * @Author: CoolPro
     * @Date: 22/3/2024
     * @Description: 请求失败时使用的方法,不携带参数,只返回状态码和状态信息
     * @Name: error
     * @parmas: [exceptionEnum]
     * @return: com.qidian.util.Result<T>
    */

    public static <T> Result<T> error(ExceptionEnum exceptionEnum) {
        Result<T> result = new Result<T>();
        result.setStatus(exceptionEnum);
        return result;
    }

    private Result() {
    }

    private Result(T data) {
        this.data = data;
    }


    public void setStatus(ExceptionEnum exceptionEnum) {
        setCode(exceptionEnum.getCode());
        setMsg(exceptionEnum.getMessage());
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}
